Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/78.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript jquery按文本选择选项工作不正常_Javascript_Jquery_Html - Fatal编程技术网

Javascript jquery按文本选择选项工作不正常

Javascript jquery按文本选择选项工作不正常,javascript,jquery,html,Javascript,Jquery,Html,在我的代码中有三个选择元素(每个文件一个),每个元素有3或4个选项。我在拥有第一个文件的行上添加了一个Apply All按钮 如果用户在第一个文件上选择图纸名称并单击“全部应用”按钮,则必须在所有文件上选择相同的图纸。如果任何文件中缺少工作表,则必须显示类似“不匹配工作表”的警报。这是我试过的 <form method="post" id="sheetForm" action="#"><input type="hidden" name="csrfmiddlewaretoken"

在我的代码中有三个
选择
元素(每个文件一个),每个元素有3或4个选项。我在拥有第一个文件的行上添加了一个Apply All按钮

如果用户在第一个文件上选择图纸名称并单击“全部应用”
按钮,则必须在所有文件上选择相同的图纸。如果任何文件中缺少工作表,则必须显示类似“不匹配工作表”的警报。这是我试过的

<form method="post" id="sheetForm" action="#"><input type="hidden" name="csrfmiddlewaretoken" value="cR9fmhJk0hhQF0FIFscTABn3DXnXMPNPAOu2cZhSwFwRfC0FleEUJnlVsqbC2I4D">
            <div class="row">
              <div class="col-sm-12">
                <div class="m-b-15">

                </div>
              </div>
            </div>

            <div class="row">
              <div class="m-b-30 form-group">
                <label class="col-md-4 control-label">Sheet Select Mode</label>
                <div class="col-md-8">
                  <label class="radio-inline">
                      <input type="radio" id="inlineRadio1" value="option1" name="radioInline">By Name
                  </label>
                  <label class="radio-inline">
                      <input type="radio" id="inlineRadio2" value="option2" name="radioInline">By Position
                  </label>
                </div>
              </div>
              <table id="tblPreview" class="table table-hover dt-responsive nowrap" cellspacing="0" width="100%">
                <thead>
                  <tr>
                    <th>File Name</th>
                    <th>Sheet Name</th>
                    <th>Actions</th>
                  </tr>
                </thead>
                <tbody>

                  <tr>
                    <td class="file-name">test-data-input-xls-mult-feb.xlsx</td>
                    <input type="hidden" name="filename" value="test-data-input-xls-mult-feb.xlsx">
                    <td>
                      <select id="select1" class="form-control input-small sheet-select" name="sheet-select">


                          <option value="name 1" selected="selected" >Sheet1</option>

                        <option value="index 1">1</option>


                          <option value="name 2">Sheet2</option>

                        <option value="index 2">2</option>

                    </select>
                    </td>
                    <td class="open">

                      <button type="button" id="btnApplyAll" class="btn btn-default dropdown-toggle" data-toggle="dropdown" aria-expanded="true">Apply All Files </button>

                    </td>

                  </tr>


                  <tr>
                    <td class="file-name">test-data-input-xls-mult-jan.xlsx</td>
                    <input type="hidden" name="filename" value="test-data-input-xls-mult-jan.xlsx">
                    <td>
                      <select id="select2" class="form-control input-small sheet-select" name="sheet-select">


                          <option value="name 1" selected="selected">Sheet1</option>

                        <option value="index 1">1</option>


                          <option value="name 2" >Sheet2</option>

                        <option value="index 2" >2</option>

                    </select>
                    </td>
                    <td>

                    </td>

                  </tr>


                  <tr>
                    <td class="file-name">test-data-input-xls-mult-mar.xlsx</td>
                    <input type="hidden" name="filename" value="test-data-input-xls-mult-mar.xlsx">
                    <td>
                      <select id="select3" class="form-control input-small sheet-select" name="sheet-select">


                          <option value="name 1" selected="selected" >Sheet1</option>

                        <option value="index 1" >1</option>

                          <option value="name 2" >Sheet2</option>

                        <option value="index 2">2</option>

                    </select>
                    </td>
                    <td>
                    </td>
                  </tr>
                </tbody>
              </table>
            </div>
          </form>

图纸选择模式
按姓名
按位置
文件名
图纸名称
行动
test-data-input-xls-mult-feb.xlsx
表一
1.
表2
2.
应用所有文件
test-data-input-xls-mult-jan.xlsx
表一
1.
表2
2.
test-data-input-xls-mult-mar.xlsx
表一
1.
表2
2.
相关的js代码如下所示

$('#btnApplyAll').on('click', function(){
    // get the selected option of first select
    var noSuchOption = false;
    var selectedOption = $('#select1').find(":selected").text();
    var selects = $('select[name="sheet-select"]');
    $('select[name="sheet-select"] option[selected="selected"]').removeAttr('selected');

    $.each(selects, function(index, select) {
      var opts = $(select).find('option').filter(function(){ return this.text == selectedOption;});
      if(opts.length < 1) {
        noSuchOption = true;
        return false;
      }
    });

    if(noSuchOption) {
      notify_long("Selected sheet doesn't exists in all files!", 'danger');
    } else {
        $('select[name="sheet-select"] option').filter(function(){
            return this.text == selectedOption;
        }).attr('selected', true);
    }
  });
$('btnApplyAll')。在('click',function()上{
//获取first select的所选选项
var noSuchOption=false;
var selectedOption=$(“#select1”).find(“:selected”).text();
var selects=$('select[name=“sheet select”]”);
$('select[name=“sheet select”]option[selected=“selected”]).removeAttr('selected');
$.each(选择,函数(索引,选择){
var opts=$(select).find('option').filter(函数(){返回this.text==selectedOption;});
如果(选择长度<1){
noSuchOption=true;
返回false;
}
});
如果(无此选项){
notify_long(“所选工作表不存在于所有文件中!”,“danger”);
}否则{
$('select[name=“sheet select”]option')。过滤器(函数(){
返回this.text==selectedOption;
}).attr('selected',true);
}
});
这段代码在3次或4次按钮单击的初始阶段起作用,但如果在中间阶段选择文件1上的sheet1、文件2上的sheet2、文件3上的sheet1后单击apply all按钮,则无法更改。此时,在单选按钮之间切换也无法显示相关选项


这可以满足您的要求:

$('btnApplyAll')。在('click',function()上{
var noSuchOption=false;
var selectedOption=null;
$('select.sheet select')。每个(函数(索引){
如果(无选择)返回;
如果(索引==0){
selectedOption=$(this.val();
返回;
}
if($(this).find('option[value=“”+selectedOption+''“])。长度==0){
noSuchOption=true;
警报(“文件:”+$(this.parent().prev().val()+“未选择工作表”,“危险”);
返回;
}
$(this).val(selectedOption);
})
});
功能切换选项(e){
var toggle=$(this.attr('id')=='inlineRadio1'?'name':'index';
$('select.sheet-select option').hide()
$('select.sheet select')。每个(函数(){
让optsToShow=$(this.find('option[value^=“”+toggle+'”);
optsToShow.show();
$(this.val(optsToShow.first().attr('value'));
});
}
$(“#inlineRadio1,#inlineRadio2”)
.更改(切换选项)
.first().change();//触发更改以初始化

图纸选择模式
按姓名
按位置
文件名
图纸名称
行动
test-data-input-xls-mult-feb.xlsx
表一
1.
表2
2.
表3
应用所有文件
test-data-input-xls-mult-jan.xlsx
表一
1.
表2
2.
test-data-input-xls-mult-mar.xlsx
表一
1.
表2
2.

这可以满足您的要求:

$('btnApplyAll')。在('click',function()上{
var noSuchOption=false;
var selectedOption=null;
$('select.sheet select')。每个(函数