duanci6484 2014-08-09 16:06
浏览 78
已采纳

无法通过JQuery访问选择框

I have a div and its content changes depending on the value of a select box above of the div(I am making and ajax call to a php page to do this and there is no problem with that). After the pages's content changed there will be an another select box inside of the div(this select box comes after first ajax response). What i want to do is making an another ajax call by using this select box's change event but i couldnt access this select box via jquery. What can be the reason of this ? Here is my implementation:

HTML:

<div id="formTypeSelectionDiv">
    <label>Select the internship type you want to edit:</label></br></br>
    <select id="formTypeSelection">
        <option value="">--select--</option>
        <option value="IENG349">IENG 349 Grades</option>
        <option value="IENG449">IENG 449 Grades</option>
        <option value="IENG349Stage2">IENG 349 Stage 2 Form</option>
        <option value="IENG449Stage2">IENG 449 Stage 2 Form</option> 
    </select>
</div>
<div id="resultDiv">
//The selectbox will be here after first ajax response and it will be like that
    <select id='fieldOfBusinessEditSelectBox'>
        <option value=''>--select--</option>
        <option value='1'>Production</option>
        <option value='2'>Service</option>
    </select> 
</div>

JQuery:

$("#fieldOfBusinessEditSelectBox").change(function(){
    var value = $("select#fieldOfBusinessEditSelectBox option:selected").val();
    alert(value);
});
  • 写回答

2条回答 默认 最新

  • dsfvsdfv23599 2014-08-09 16:08
    关注

    Event handlers are bound only to the currently selected elements; they must exist on the page at the time your code makes the event binding call.

    Delegated events have the advantage that they can process events from descendant elements that are added to the document at a later time.

    As you are loading using AJAX.

    You need to use Event Delegation. You have to use .on() using delegated-events approach.

    General Syntax

    $(document).on(event, selector, eventHandler);
    

    As per your code

    $(document).on('click', '#fieldOfBusinessEditSelectBox', function(){
        var value = $(this).val();
    });
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 求帮我调试一下freefem代码
  • ¥15 matlab代码解决,怎么运行
  • ¥15 R语言Rstudio突然无法启动
  • ¥15 关于#matlab#的问题:提取2个图像的变量作为另外一个图像像元的移动量,计算新的位置创建新的图像并提取第二个图像的变量到新的图像
  • ¥15 改算法,照着压缩包里边,参考其他代码封装的格式 写到main函数里
  • ¥15 用windows做服务的同志有吗
  • ¥60 求一个简单的网页(标签-安全|关键词-上传)
  • ¥35 lstm时间序列共享单车预测,loss值优化,参数优化算法
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?