onclick按钮添加文本字段无效

时间:2019-05-29 10:22:33

标签: javascript php jquery html

单击按钮时,我正在使用onclick事件添加文本字段。它适用于时间div(请参见下图),但不适用于拾音器div。

enter image description here

JS:

// time ---------------------------

function clickedfun(id) {

    var count = jQuery('#total_time_rec'+id).val();
    console.log('id'+id);
    count++;

    jQuery('#here'+id).append('<div class="time_slot"><input type="text" name="_pro_service_price['+id+'][time]['+count+']" placeholder="HH:ii" class="services_time"/><div class="remove_img remove_shortcode" id="">X</div></div><script>jQuery(".services_time").timepicker();</script>');

    jQuery('#total_time_rec').val(count);

    return false;
};

// pickup ---------------------------

function clickedfun2(id) {

    var pickupcount = jQuery('#total_pickup_rec'+id).val();
    console.log('id'+id);
    pickupcount++;

    jQuery('#pickuphere'+id).append('<div class="pickup_slot"><input type="text" name="_pro_service_price['+id+'][pickup]['+pickupcount+']" placeholder=""/><div class="remove_img remove_shortcode" id="">X</div></div>');

    jQuery('#total_pickup_rec').val(pickupcount);

    return false;
};

PHP内的HTML:

echo '<!-- --------------- time code starts here --------------- -->
<tr class="label_tr">
    <td class="label_title"><label for="_pro_service_price['.$id.'][is_active]">Time</label></td>
    <td style="width: 40%;">';
    if(!empty($value->time)){
        $time_slots = $value->time;
        $count = count($time_slots);

        foreach ($time_slots as $key => $time) {
            echo '<div class="time_slot"><input type="text" name="_pro_service_price['.$id.'][time]['.$key.']" placeholder="HH:ii" class="services_time" value="'.$time.'"/>
            <div class="remove_img remove_shortcode" id="">X</div>
            </div>';
        }
    }else{
        echo '<div class="time_slot"><input type="text" name="_pro_service_price['.$id.'][time][1]" placeholder="HH:ii" class="services_time"/>
            <div class="remove_img remove_shortcode" id="">X</div>
        </div>';
        $count = 1;
    }

        echo '<div id="here'.$id.'"></div>
        <div class="add_img button button-primary button-large" id="add_shortcode" onclick="clickedfun('.$id.')">Add Time</div>
        <input type="hidden" value="'.$count.'" id="total_time_rec'.$id.'">
    </td>
    <td></td>
</tr>

<!-- --------------- pickup code starts here --------------- -->
<tr class="label_tr">
    <td class="label_title"><label for="_pro_service_price['.$id.'][is_active]">Pickup</label></td>
    <td style="width: 40%;">';
    if(!empty($value->pickup)){
        $pickup_slots = $value->pickup;
        $pickupcount = count($pickup_slots);

        foreach ($pickup_slots as $key => $pickup) {
            echo '<div class="pickup_slot"><input type="text" name="_pro_service_price['.$id.'][pickup]['.$key.']" placeholder="" value="'.$pickup.'"/>
            <div class="remove_img remove_shortcode" id="">X</div>
            </div>';
        }
    }else{
        echo '<div class="pickup_slot"><input type="text" name="_pro_service_price['.$id.'][pickup][1]" placeholder=""/>
            <div class="remove_img remove_shortcode" id="">X</div>
        </div>';
        $pickupcount = 1;
    }

        echo '<div id="pickuphere'.$id.'"></div>
        <div class="add_img button button-primary button-large" id="add_shortcode2" onclick="clickedfun2('.$id.')">Add Pickup</div>
        <input type="hidden" value="'.$pickupcount.'" id="total_pickup_rec'.$id.'">
    </td>
    <td></td>
</tr>';

从Inspect元素渲染的HTML:

<!-- time code starts here -->
<tr class="label_tr">
    <td class="label_title"><label for="_pro_service_price[9][is_active]">Time</label></td>
    <td style="width: 40%;"><div class="time_slot"><input type="text" name="_pro_service_price[9][time][1]" placeholder="HH:ii" class="services_time ui-timepicker-input" value="12:00am" autocomplete="off">
            <div class="remove_img remove_shortcode" id="">X</div>
            </div><div id="here9"><div class="time_slot"><input type="text" name="_pro_service_price[9][time][2]" placeholder="HH:ii" class="services_time ui-timepicker-input" autocomplete="off"><div class="remove_img remove_shortcode" id="">X</div></div><script>jQuery(".services_time").timepicker();</script><div class="time_slot"><input type="text" name="_pro_service_price[9][time][2]" placeholder="HH:ii" class="services_time ui-timepicker-input" autocomplete="off"><div class="remove_img remove_shortcode" id="">X</div></div><script>jQuery(".services_time").timepicker();</script></div>
        <div class="add_img button button-primary button-large" id="add_shortcode" onclick="clickedfun(9)">Add Time</div>
        <input type="hidden" value="1" id="total_time_rec9">
    </td>
    <td></td>
</tr>

<!-- pickup code starts here -->
<tr class="label_tr">
    <td class="label_title"><label for="_pro_service_price[9][is_active]">Pickup</label></td>
    <td style="width: 40%;"><div class="pickup_slot"><input type="text" name="_pro_service_price[9][pickup][1]" placeholder="" value="test pickup entrance 1">
            <div class="remove_img remove_shortcode" id="">X</div>
            </div><div id="pickuphere9"></div>
        <div class="add_img button button-primary button-large" id="add_shortcode2" onclick="clickedfun2(9)">Add Pickup</div>
        <input type="hidden" value="1" id="total_pickup_rec9">
    </td>
    <td></td>
</tr>

控制台日志:

Uncaught ReferenceError: clickedfun2 is not defined
    at HTMLDivElement.onclick (post.php?post=126&action=edit:2040)

0 个答案:

没有答案