如何在jQuery中获得相同的效果?

时间:2016-08-21 19:02:31

标签: jquery select

我有这个用警报识别“id”但不起作用。

jQuery(document).ready(function($) {
    $('.rating-button').click(function(e){
        e.preventDefault();


        var form = $(this).find('form');
        var id = form.attr('id');

    alert(id);
    });
});

HTML

<form onsubmit="return validClear('crform34') && recargarCitasEnvi()" action="http://www.fastdate.es/controladores/grabarcita.php" name="crform34" name="crform34" target="done" method="post">
<div class="crintro">
<br>
<h6>
<input id="crsite" type="text" value="" maxlength="250" name="crsite">
<div id="addr">
<h6>
<input id="crcity" type="text" value="" maxlength="250" name="crcity">
<h6>
<input id="crcpostal" type="text" value="" maxlength="250" name="crcpostal">
<h6>
<input id="crfecha" class="datepicker" type="text" name="crfecha">
<h6>
<input id="crsecret" type="text" value="" maxlength="250" name="crsecret">
<h6>
<textarea id="crcomentario" rows="3" cols="20" name="crcomentario"></textarea>
<br>
<input type="hidden" value="34" name="idreceptor">
<input type="hidden" value="21" name="idcitador">
<input type="hidden" value="eleven" name="namereceptor">
<input type="hidden" value="Maiers" name="namecitador">
<input type="hidden" value="http://www.fastdate.es/profile/maiers/" name="urlcitador">
<input class="rating-button" type="submit" value="Enviar cita">
</form>

如何使用jQuery获得相同的效果? 当“id”是变量值时。

我希望在同一页面中使用不同ID的多个表单之间选择一个表单。

1 个答案:

答案 0 :(得分:0)

这是基本的jQuery,你所做的大部分工作看起来都是正确的..你只需要定位你找到的ID ...然后添加课程......

jQuery(document).ready(function($) {
    $('.rating-button').click(function(e){
        e.preventDefault();


        var form = $(this).find('form');
        var id = form.attr('id');
        $(id).addClass('showing');
    });
});

...或

调整HTML:

<form onsubmit="return validClear('crform34') && recargarCitasEnvi()" action="http://www.fastdate.es/controladores/grabarcita.php" name="crform34" name="crform34" target="done" method="post" id="[INSERT A UNIQUE ID HERE]">

<input class="rating-button" type="submit" data-id="[INSERT FORM ID HERE]" value="Enviar cita">

然后调用该数据......

如果您更正问题中的无效标记(关闭h6 and div`标签)...这似乎工作正常...

&#13;
&#13;
jQuery(document).ready(function($) {
    $('.rating-button').click(function(e){
        e.preventDefault();

        var form = $(this).attr('data-id');
        alert(form);

    });
});
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<form onsubmit="" action="" name="crform34" name="crform34" target="done" method="post" id="form1">
<div class="crintro">
<br />
<h6>
<input id="crsite" type="text" value="" maxlength="250" name="crsite" />
</h6>
<div id="addr">
<h6>
<input id="crcity" type="text" value="" maxlength="250" name="crcity" />
</h6>
<h6>
<input id="crcpostal" type="text" value="" maxlength="250" name="crcpostal" />
</h6>
</div>
<h6>
<input id="crfecha" class="datepicker" type="text" name="crfecha" />
</h6>
<h6>
<input id="crsecret" type="text" value="" maxlength="250" name="crsecret" />
</h6>
<h6>
<textarea id="crcomentario" rows="3" cols="20" name="crcomentario"></textarea>
</h6>
<br />
<input type="hidden" value="34" name="idreceptor" />
<input type="hidden" value="21" name="idcitador" />
<input type="hidden" value="eleven" name="namereceptor" />
<input type="hidden" value="Maiers" name="namecitador" />
<input type="hidden" value="http://www.fastdate.es/profile/maiers/" name="urlcitador" />
<input class="rating-button" type="submit" data-id="form1" value="Enviar cita" />
</div>
</form>
&#13;
&#13;
&#13;

您遇到的最大问题是您正在尝试根据其ID来选择表单..但在您的标记中没有ID