简体   繁体   English

是否可以使用jQuery重命名按钮的名称,以根据按钮的名称执行不同的php脚本?

[英]Is it possible to rename a button's name with jQuery to execute different php-scripts depending on the button's name?

I got a script with a navigation list (monday to sunday) and i wanna change a button's name depending on which day has "active"-class. 我有一个带有导航列表的脚本(星期一到星期日),我想根据哪一天有“活动”类更改按钮的名称。 I know how to select the element with the active class (and how to change the class). 我知道如何选择具有活动类的元素(以及如何更改类)。 To receive the active class' name I use the following code: 要接收活动类的名称,请使用以下代码:

var day=$('#navi ul li a.active').html();

But I was not able to change the name of the submit button. 但我无法更改提交按钮的名称。 I tried this: 我试过这个:

$('#loadStandard').name = $('#loadStandard').name + "_" + day;

This didnt change anything. 这没有任何改变。 I also tried: 我也尝试过:

$('#loadStandard').name= "bla";

but it didnt work. 但它没有用。 According to this it's the correct syntax. 根据这个,这是正确的语法。 My form looks like this: 我的表单看起来像这样:

<form action="<?php echo plugins_url('plugin_directory/my_plugin.php'); ?>" method="post">
<input type="submit" name="loadStandard" class="loadStandard" id="loadStandard" value="Load standard values">
</form>

I want to change the name of the button so I can make different if statements in my php script. 我想更改按钮的名称,以便我可以在我的PHP脚本中创建不同的if语句。 Kinda like this: 有点像:

if (isset($_POST['loadStandard_monday'])) {
    do something;
}
elseif(isset($_POST['loadStandard_tuesday'])){
    do something else;
}

Is it even possible? 它甚至可能吗? What am I doing wrong? 我究竟做错了什么? Please help me with some tips, links or code snippets! 请帮我提供一些提示,链接或代码片段!

Like the comments mention, you need to use .attr('name', day) . 就像提到的评论一样,你需要使用.attr('name', day)

Or may I suggest you just use a hidden value instead? 或者我可以建议您只使用隐藏值吗?

<input type="hidden" name="loadStandard[day]"  id="loadStandard_day" value="" />

$('#loadStandard_day').val(day);

<?php
if ($_POST['loadStandard']['day'] == '...')
    //...

Strange approach. 奇怪的方法。 You can do it like this: 你可以这样做:

var day=$('#navi ul li a.active').html(),
    submit_name = $('#loadStandard').attr('name');
$('#loadStandard').attr('name', submit_name+ "_" + day);

It will be better if you will use some hidden field: 如果你使用一些隐藏的字段会更好:

<input type="hidden" name="day" id="id-day" value=""/>

var day=$('#navi ul li a.active').html();
    $('#id-day').val(day);

and on the server side: 在服务器端:

switch($_POST['day']) {
   case "sunday":
      ...
      break;
   case "monday":
      ...
      break;
   ...
}

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

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