Javascript中的动态值

时间:2016-01-05 12:38:23

标签: javascript php jquery

我有:

function saved() {
    $('#linha_1').append('<div class="salvo"><img width=20 height=20 src="http://www.toppromomkt.com/wp-content/themes/toppromo/images/check1.png"></div>'); 
}

我的表单是在具有不同ID的动态循环中生成的(不仅#linha_1而且#linha_2等。我该如何制作我的按钮:

<input type='Submit' value='Salvar' onclick='saved();'>

将正确的ID传递给我的Javascript?那么我可以拥有动态值而不是$('#linha_1')而不是linha_$events[0]吗?在我的情况下,我使用div生成我要追加的<form action='http://www.toppromomkt.com/wp-content/themes/toppromo/insert_wallet.php' method='post' target='hidden-form'> <input type='checkbox' name='pago' value='yes' <?php if ($che=='yes') {echo "checked='yes'";} else if ($cccc='') {} else {} ; ?> > <br> </div><div class='tb1_small sc' > <input type='checkbox' name='recibo' value='yes' <?php if ($rec=='yes') {echo "checked='yes'";} else{}; ?> > <br></div><div class='tb3_md sc '> <input type="hidden" name="contador" readonly value="<?php echo $id_postt1."linha_".$events[0]; ?>"> <input type="hidden" name="id_p" readonly value="<?php echo $events[2];?>"> <input type='Submit' value='Salvar' class='my-form' onclick='saved();'> </form> </div></div> ID ...

形成循环:

016-01-05 00:39:11,283 DEBUG [thrift-worker-5] thrift.ThriftServerRunner$HBaseHandler: scannerGetList: id=116
2016-01-05 00:39:31,798 DEBUG [thrift-worker-4] thrift.ThriftServerRunner$HBaseHandler: scannerGetList: id=117
2016-01-05 00:39:32,449 DEBUG [thrift-worker-4] thrift.ThriftServerRunner$HBaseHandler: scannerGetList: id=117
2016-01-05 00:40:10,968 INFO  [ConnectionCleaner] client.HConnectionManager$HConnectionImplementation: Closing zookeeper sessionid=0x352103c1cb50008
2016-01-05 00:40:10,975 INFO  [ConnectionCleaner] zookeeper.ZooKeeper: Session: 0x352103c1cb50008 closed
2016-01-05 00:40:11,003 INFO  [thrift-worker-0-EventThread] zookeeper.ClientCnxn: EventThread shut down

3 个答案:

答案 0 :(得分:1)

像这样使用:

function saved(id) {  // receive id of the form on which saved is clicked
    $('#linha_'+ id).append('<div class="salvo"><img width=20 height=20 src="http://www.toppromomkt.com/wp-content/themes/toppromo/images/check1.png"></div>'); 
}

在每个循环中都这样做:

<input type='Submit' value='Salvar' onclick='saved(<?php echo $events[0]); ?>'> // pass id of the form to javascript function here

答案 1 :(得分:1)

更好的方法是不使用ID,而是使用类:

<form id="form_1" class="my-form">
    <!-- ... -->
    <button type="submit">Save</button>
</form>
...
<form id="form_n" class="my-form">
    <!-- ... -->
    <button type="submit">Save</button>
</form>

然后你可以这样做:

$('.my-form').on('submit', function(e) {
    e.preventDefault(); // remove this if your are not using ajax form handlers...

    // handle the submission as you want... To get the form id, do:
    var id = $(this).attr('id');

    $(this).append('<div class="salvo"><img width=20 height=20 src="http://www.toppromomkt.com/wp-content/themes/toppromo/images/check1.png"></div>');
});

答案 2 :(得分:0)

请勿使用单独的功能并使用on*属性进行调用。相反,不引人注意地使用它。给一个班级<input>form-submit

<input type='Submit' value='Salvar' class='form-submit' />

然后像这样称呼它:

$('.form-submit').closest('form').submit(function (e) {
  e.preventDefault();
  $(this).append('<div class="salvo"><img width=20 height=20 src="http://www.toppromomkt.com/wp-content/themes/toppromo/images/check1.png"></div>');
});