【问题标题】:Can't trigger change on option with CasperJS无法使用 CasperJS 触发选项更改
【发布时间】:2016-12-07 14:20:15
【问题描述】:

我有这个 html:

<select class="ik-target" name="make_id" id="the_maker_id" data-ik-class="maker-select">
 <option value="" class="empty-item">Choose maker</option>
 <optgroup label="Some of them">
  ...
 </optgroup>
 <optgroup label="Chosen">
    <option value="2" style="text-indent: 10px;">A</option>
    <option value="5" style="text-indent: 10px;">B</option>
    <option value="16" style="text-indent: 10px;">C</option>
    <option value="819" style="text-indent: 10px;">D</option>
 </optgroup>
</select>

然后我试试这个(通常有效):

var thisnow = "5";
this.evaluate(function(valueOptionSelect){
   $('select#form_maker_id optgroup[label="Chosen"] option').val(valueOptionSelect);
   $('select#form_maker_id optgroup[label="Chosen"] option').trigger("change");
},thisnow);

但这不会改变默认value=""的选项值

有什么办法可以解决吗?

【问题讨论】:

  • 首先,您应该在select 上触发change,而不是option(s)。其次,.val()
  • @A.Wolff 喜欢这样吗? $('select#form_maker_id optgroup[label="Chosen"]')?不工作:/
  • 不,我的意思是例如:$('#form_maker_id').val(valueOptionSelect);$('#form_maker_id').trigger('change');,顺便说一句,这可以链接:$('#form_maker_id').val(valueOptionSelect).change();
  • @A.Wolff 效果不佳:/
  • @A.Wolff select 是在一个表单中,也许是关于它的?该表单上没有method='get'

标签: jquery html-select casperjs


【解决方案1】:

你应该设置select元素的值并触发change事件:

$('#form_maker_id').val(valueOptionSelect).change();

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-07-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-10-20
    • 2020-10-27
    • 2014-08-02
    • 2014-11-20
    相关资源
    最近更新 更多