【问题标题】:Get dropdown selected value from jquery template in table row从表格行中的jquery模板获取下拉选择值
【发布时间】:2015-05-11 07:46:09
【问题描述】:

我有以下 jquery 模板,我想在单击按钮时从下拉框中获取选定的值。

Jquery 模板:

<script id="template-download" type="text/x-jquery-tmpl">
        <tr class="template-download{{if _errorMSG}} ui-state-error{{/if}}">
            {{if _errorMSG}}
                <td></td>
                <td class="name">${_name}</td>
                <td class="size">${sizef}</td>
                <td class="error" colspan="2">Error:
                    {{if _errorMSG === 1}}File exceeds upload_max_filesize
                    {{else _errorMSG === 2}}File exceeds MAX_FILE_SIZE (HTML form directive)
                    {{else _errorMSG === 3}}File was only partially uploaded
                    {{else _errorMSG === 4}}No File was uploaded
                    {{else _errorMSG === 5}}Missing a temporary folder
                    {{else _errorMSG === 6}}Failed to write file to disk
                    {{else _errorMSG === 7}}File upload stopped by extension
                    {{else _errorMSG === 'maxFileSize'}}File is too big
                    {{else _errorMSG === 'minFileSize'}}File is too small
                    {{else _errorMSG === 'acceptFileTypes'}}Filetype not allowed
                    {{else _errorMSG === 'maxNumberOfFiles'}}Max number of files exceeded
                    {{else _errorMSG === 'uploadedBytes'}}Uploaded bytes exceed file size
                    {{else _errorMSG === 'emptyResult'}}Empty file upload result
                    {{else}}${_errorMSG}. File Not Uploaded!
                    {{/if}}
                </td>
                <td class="failupload">
                    <button data-type="${delete_type}" data-url="${delete_url}">Cancel Upload</button>
                </td>                
            {{else}}
                <td class="preview">
                    {{if Thumbnail_url}}
                        <a href="${_url}" target="_blank"><img src="${Thumbnail_url}"></a>
                    {{/if}}
                </td>
                <td class="name">
                    <a href="${_url}"{{if thumbnail_url}} target="_blank"{{/if}}>${_name}</a>
                </td>
                <td class="size">${sizef}</td>
                <td colspan="2"></td>
                <td>
                    <select class="fileupload-buttonbar" id='${_name}'>
                        <option value="1">Fundus</option>
                        <option value="0">Other</option>
                    </select>
                </td>
                <td class="success">
                    <button class="linkfile">Link File</button>
                </td>
                <td class="delete">
                    <button data-type="${delete_type}" data-url="${delete_url}">Remove File</button>
                </td>                
            {{/if}}

        </tr>
</script>

我曾尝试使用$(this).parent().prev().children().find(":selected").text(),但这也不起作用。

任何建议将不胜感激

编辑:

这是一个测试提供的代码的函数。

$(document).ready(function () {
    $('.linkfile').on('click', function () {
        alert($(this).closest('tr').find('.fileupload-buttonbar option:selected').val());
    });
});

【问题讨论】:

    标签: javascript jquery


    【解决方案1】:

    您应该使用val() 来获取所选选项的值。

    $(this).closest('tr').find('.fileupload-buttonbar').val()
    

    【讨论】:

    • 你不需要使用option:selected
    • @Tushar,我已经更新了我的问题以使用您提供的代码。不确定问题是否出在按钮的单击事件上,但它不起作用。
    【解决方案2】:

    您需要使用.val() 以及选择元素选择器来获取选定的值:

    $(this).closest('tr').find("select").val()
    

    【讨论】:

      【解决方案3】:

      您应该使用Event Delegation 使用.on() 委托事件方法。以及.val() 方法。

      $(document).ready(function () {
          $(document).on('click', '.linkfile', function () {
              alert($(this).closest('tr').find('.fileupload-buttonbar').val());
          });
      });
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2018-12-21
        • 1970-01-01
        • 1970-01-01
        • 2023-03-09
        • 1970-01-01
        • 2011-07-06
        相关资源
        最近更新 更多