简体   繁体   English

为一个隐藏的输入字段获取多个值

[英]get multiple values for one hidden input field

I have 2 selectable options, what I need is some how to get the value from the selected and add it to the hidden field so when submit is clicked it takes the value into the php and depending on the value each one will have a different outcome to an mysql database insert/update. 我有2个可选选项,我需要的是一些如何从选定的值中获取值并将其添加到隐藏字段中的功能,因此,当单击Submit时,它将值带入php中,并且根据值,每个值都会有不同的结果到mysql数据库的插入/更新。

I have it working with code I have but...It is getting the 1st option every time by default, It is not getting a value depending on what is selected. 我有它与我拥有的代码一起使用,但是...默认情况下,它每次都会获取第一个选项,它不会根据选择的内容获取值。

JS JS

$(document).ready(function(){
$(".crimeSelect").hover(
    function() {
        if ($(this).hasClass('isSelected') == false) { $(this).css('opacity', 0.80); }
    }
);
$(".crimeSelect").click(function() {
    $("#selec1").val($(this).attr('crimeid'));
    $(this).css('opacity', 1.00);
    $('.crimeSelect.isSelected').removeClass('isSelected').css('opacity', 0.40);
    $(this).addClass('isSelected');

});
});

HTML 的HTML

<td class="content">
  <input type="hidden" value="x" name="Crime" id="selec1"/>
  <div crimeid="9" name="9" class="crimeSelect" style="cursor:pointer;opacity:0.40;margin:auto;"><img src="images/crimes/Kidnapthepope-Title.png" style="width:100%;height:100%;margin:auto;"/></div>
</td>
<td class="content">
  <div crimeid="10" name="10" class="crimeSelect" style="cursor:pointer;opacity:0.40;margin:auto;"><img src="images/crimes/Pickpocketadoctor-Title.png" style="width:100%;height:100%;margin:auto;"/></div>
</td>

PHP 的PHP

if(isset($_POST['Crime'])) {
mysqli_query($conn, "UPDATE Account_Character SET money='0' WHERE username="$username") or die(mysqli_error());

The PHP is just a small example but this is what I am having the issue with, putting value from selected into hidden input then passing to the PHP. PHP只是一个小例子,但这就是我遇到的问题,将所选内容的值放入隐藏的输入中,然后传递给PHP。

Thanks. 谢谢。

try this 尝试这个

$(document).ready(function(){



   $(".crimeSelect").click(function(event) {
      alert($(event.target).attr('crimeid'));
       $("#selec1").val($(event.target).attr('crimeid'));
       $(this).css('opacity', 1.00);
        $('.crimeSelect.isSelected').removeClass('isSelected').css('opacity', 0.40);
       $(this).addClass('isSelected');

         });
     });

Ok so with loads of messing about was one line of code had to do 好的,所以要花很多时间写一堆代码

$(".crimeSelect").click(function() {
    $("#selec1").val($(this).attr('crimeid'));
    $(this).css('opacity', 1.00);
    $('.crimeSelect.isSelected').removeClass('isSelected').css('opacity', 0.40);
    $(this).addClass('isSelected');
    $('input[name=Crime]').val($(this).attr('crimeid'));

});

Then from the PHP pick up the HTML input name to check value and get going from there :) 然后从PHP中选取HTML输入名称以检查值并从那里开始:)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM