1

jqueryを使用して値を渡し、それをフォームの非表示のテキストボックスに割り当てることについて質問したいだけです。これが私のコードです。あなたが私を助けてくれることを願っています。

**homepage.php**
 <?php
    $cat_id = $row['salescatid'];
 ?>
 <input type="button" name="add_comment" data-value="<?php echo $cat_id; ?>" value="Add Comment" class="comment" />
 .
 .
 <div id="comment_category">
 <!-- DISPLAY COMMENT HERE -->
 </div>
 <?php echo form_open('CATEGORY_CONTROLLER/INSERT_COMMENT'); ?>
 <div id="comment_add" style="display: none;">
    <input type="hidden" value="" name="cat_id" /> //Here's the problem how can i get the value from my jquery?
    <input type="hidden" value="sales_category" name="type"/>
    <label>Write your comment</label><br />
    <textarea name="comment_category" style="width: 50%; resize: none;"></textarea><br />
    <input type="submit" value="COMMENT" class="btn btn-primary btn-SMALL"/>
 </div>
 <?php echo form_close(); ?>
 .
 .
 .
 //Here's my jquery
 $(".comment").click(function(){
       var id = $(this).attr('data-value'); //This is the value of the button, problem is how to pass it in my comment form?
       $("#comment_add") .show();
       //alert(id);
 });

みんな助けてください。ありがとう。

4

3 に答える 3

3

.val()を使用して入力要素に値を割り当て、属性セレクターを使用して name 属性を使用して入力フィールドを選択できます。

$('input[name="cat_id"]').val($(this).attr('data-value'))

元:

 $(".comment").click(function(){
       var id = $(this).attr('data-value'); //This is the value of the button, problem is how to pass it in my comment form?
       $('input[name="cat_id"]').val(id)
       $("#comment_add") .show();
       //alert(id);
 });
于 2013-07-22T03:01:31.390 に答える
1
$('input[name="cat_id"]').val(id);
于 2013-07-22T03:01:38.453 に答える
0

簡単に選択できるように、要素に id を追加することをお勧めします。

<input type="hidden" value="" id="cat_id" name="cat_id" />

または、他の回答で提案されているように名前で選択することもできます。その後、jQuery を使用してその値を簡単に設定できます。

var id = $(this).attr('data-value');
$("#cat_id").val(id);

お役に立てれば!

于 2013-07-22T03:04:21.200 に答える