简体   繁体   English

带有Javascript和PHP的多个ID

[英]Multiple ID with Javascript and PHP

I have to do a simple work. 我必须做一个简单的工作。

I have: 我有:

echo' <div class="col-sm-12" id="recensioni_titolo">
                        <form role="form" id="review-form" method="post" action="php\insert_comment.php">
                            <div class="row">

                                <div class="col-sm-8">
                                    <div class="form-group">
                                        <input type="text" class="form-control" name="Titolo" id="titolo_review" placeholder="Titolo">
                                        <input type="text" class="form-control" name="ID_locale" id="titolo_review" value="'.$id_Local.'" style="visibility: hidden; position:fixed;">
                                    </div>
                                </div>
                                <div class="col-sm-4">
                                    <fieldset class="rating">
                                        <input type="radio" id="star5'.$id_Local.'" name="Voto" value="5" /><label class = "full" for="star5'.$id_Local.'" title="Ottimo - 5 stelle"></label>
                                        <input type="radio" id="star4'.$id_Local.'" name="Voto" value="4" /><label class = "full" for="star4'.$id_Local.'" title="Buono - 4 stelle"></label>
                                        <input type="radio" id="star3'.$id_Local.'" name="Voto" value="3" /><label class = "full" for="star3'.$id_Local.'" title="Discreto - 3 stelle"></label>
                                        <input type="radio" id="star2'.$id_Local.'" name="Voto" value="2" /><label class = "full" for="star2'.$id_Local.'" title="Insufficiente - 2 stelle"></label>
                                        <input type="radio" id="star1'.$id_Local.'" name="Voto" value="1" /><label class = "full" for="star1'.$id_Local.'" title="Pessimo - 1 stella"></label>
                                    </fieldset>
                                </div>
                            </div>

                            <div class="row">
                                <div class="col-md-12">
                                    <div class="form-group">
                                        <textarea class="form-control textarea" rows="3" name="Review" id="review" placeholder="Inserisci una descrizione.."></textarea>
                                    </div>
                                </div>
                            </div>

                            <div class="row">
                                <div class="col-md-12">
                                    <button type="button" class="btn main-btn pull-right" name="submit_review" id="submit_review'.$id_Local.'">Invia</button>
                                </div>

                            </div>

This code is generating the same thing for 5 times. 这段代码生成相同的东西5次。 I'have to find one method to declare and use the id="review-form" in a unique mode because with this code: 我必须找到一种方法来声明并在唯一模式下使用id =“ review-form”,因为使用以下代码:

$(document.getElementsByName("submit_review")).unbind().click(function() {
var chx = document.getElementsByName("Voto");
   for (var i=0; i<chx.length; i++) {
// If you have more than one radio group, also check the name attribute
// for the one you want as in && chx[i].name == 'choose'
// Return true from the function on first match of a checked item
if (chx[i].type == 'radio' && chx[i].checked) {
        $.ajax({
        url : "php/insert_comment.php",
        type : "post",
        data : $("#review-form").serialize(),
        success : function(data){
            $.ajax({
        url : "php/reviews.php",
        type : "post",
        data: {'id_Local' : $('.modal').attr('data-modal')},
        success : function(data){
            $('#box_recensioni').html(data);
            chx[i].checked=false;

    }
    })
    }
   })

    return true;
   } 
 }
 // End of the loop, return false
alert("Inserisci almeno il voto!!")
return false;
});

I have only the first element is working. 我只有第一个要素在起作用。 I can generate the id with "id'.$variable'" but I don't know how to refers to every single id in the javascript file. 我可以使用“ id'。$ variable'”生成ID,但是我不知道如何引用JavaScript文件中的每个ID。

Thank you to all in advance 谢谢大家

In HTML, an ID is supposed to be unique : 在HTML中, ID应该是唯一的:

The id global attribute defines a unique identifier (ID) which must be unique in the whole document . id全局属性定义一个唯一标识符(ID),该标识符在整个文档中必须是唯一的

This is why JavaScript can grab only the first occurence of an ID, since it's supposed to be the only one. 这就是为什么JavaScript只能捕获ID的第一次出现的原因,因为它应该是唯一的。 You may want to replace those multiple IDs with classes, which is at least correct in HTML5 and will be also smarter in Javascript. 您可能想要用类替换这些多个ID,这至少在HTML5中是正确的,而在Javascript中也更聪明。

Here is a link to the post in the Mozilla documentation of IDs in HTML, to be sure that you understand the role of this tag. 这是指向 Mozilla HTML文档ID的文章的链接 ,以确保您了解此标记的作用。

You can create an array, then use an loop through each radio button pushing the id into the array. 您可以创建一个数组,然后使用遍历每个单选按钮的循环将id推入数组。 Then use the array for the ids. 然后使用数组作为ID。

 var radioIds = new Array(); $('.rating input[name="Voto"]').each(function(){ radioIds.push($(this).attr('id')); }); console.log(radioIds) 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <div class="col-sm-4"> <fieldset class="rating"> <input type="radio" id="star54" name="Voto" value="5" /><label class="full" for="star54" title="Ottimo - 5 stelle"></label> <input type="radio" id="star44'" name="Voto" value="4" /><label class="full" for="star44" title="Buono - 4 stelle"></label> <input type="radio" id="star34" name="Voto" value="3" /><label class="full" for="star34" title="Discreto - 3 stelle"></label> <input type="radio" id="star24'" name="Voto" value="2" /><label class="full" for="star24" title="Insufficiente - 2 stelle"></label> <input type="radio" id="star14" name="Voto" value="1" /><label class="full" for="star14" title="Pessimo - 1 stella"></label> </fieldset> </div> 

As other's have said using the same ID would be useless instead you may want to use a class identifier. 正如其他人所说,使用相同的ID会毫无用处,相反,您可能想使用类标识符。 And assuming you want to create multiple form elements... and you don't have a unique identifier to use in your scripts you may try the following which I haven't tested... 并假设您要创建多个表单元素...并且您没有在脚本中使用的唯一标识符,则可以尝试以下我未测试的...

I see you have already gotten the radio button 我看到您已经按下了单选按钮

 var chx = document.getElementsByName("Voto"); 

and performed a check on it under your if statement 并在您的if语句下进行了检查

 if (chx[i].type == 'radio' && chx[i].checked) { 

if so, maybe you can try to get the closest form element of the radio button that you are dealing with (ie checked) by doing something like 如果是这样,也许您可​​以通过执行以下操作来尝试获取要处理(即选中)的单选按钮的最接近表单元素

 var thisForm = chx[i].closest('form') --and later do thisForm.serialize(); 

check this out for more detail in using closest 在使用最接近功能时,请查看更多信息

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

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