![](/img/trans.png)
[英]Check if element has a certain class and belongs to a form that contains a certain value
[英]Automatically add a certain class, whenever <Form> code contains a certain value?
我想向每個包含special
字詞的表單添加一個特殊類,作為其操作URL的一部分。
但是我有很多形式,我想不出一種自動的方式來做到這一點。
我想到的唯一方法是創建以下代碼,但是我將必須使用不同的索引號為每個表單創建這樣的代碼:
<?php
$case1 = "special";
$case2 = "none";
if($case1 == "none") {
$class1 = ""; // don't add any class
}
else {
$class1 = "effect"; // add `effect` class
}
if($case2 == "none") {
$class2 = ""; // same goes here
}
else {
$class2 = "effect"; // and here
}
?>
HTML:
<form action="/go/<?= $case1 ?>" method="POST" target="_blank">
<input type="submit" class="<?= $class1 ?> general-class" value="submit">
</form>
<form action="/go/<?= $case2 ?>" method="POST" target="_blank">
<input type="submit" class="<?= $class2 ?> general-class" value="submit">
</form>
OUTPUT:
<form action="/go/special" method="POST" target="_blank">
<input type="submit" class="effect general-class" value="submit">
</form>
<form action="/go/none" method="POST" target="_blank">
<input type="submit" class="general-class" value="submit">
</form>
有沒有自動的方法可以做到這一點?
基本上,我有兩種形式,一種應使用jquery
插件(特殊類)打開,另一種應以常規方式打開。
我區分它們的方法是將$case[i]
變量插入到動作url中,因為這是我必須做的兩種方式。
我不知道,也許有一種完全不同的方法可以實現這一目標。
編輯:
真正的表單主要是通過以下代碼手動生成的:
<form action="/go/<?= $item ?>/<?php echo $case1 ; ?>" method="POST" target="_blank">
<input name="a" type="hidden" value="<?php echo $a; ?>"/>
<input name="b" type="hidden" value="<?php echo $b; ?>"/>
<input name="c" type="hidden" value="<?php echo $c; ?>"/>
<input name="d" type="hidden" value="<?php echo $d; ?>"/>
<input type="submit" class="<?= $class1 ?> general-class" value="Click Me"></form>
(在上面看到的PHP塊的開頭,為所有變量提供了值)
有兩種方法-PHP和jQuery。 根據您的代碼,我們沒有足夠的信息來知道是否可以使用PHP方式,因此這是jQuery方式:
// wait until the document is ready
jQuery(function($) {
// find all forms that have "special" in the action
$('form[action*="special"]').each(
function() {
// within the form, find the submit button, and add the "effect" class
$(this).find('input[type="submit"]').addClass('effect');
}
);
});
而且,簡短/較不冗長的方式是:
jQuery(function($) {
// find all forms that have "special" in the action, find their input, and add the class
$('form[action*="special"] input[type="submit"]').addClass('effect');
});
因此,要在PHP中執行此操作,我建議編寫一個簡單的函數,然后調用它。
function get_class( $slug ) {
$class_map = array(
'special' => 'effect',
'none' => '',
// .. you could add others here if appropriate
return ( isset( $class_map[ $slug ] ) ) ? $class_map[ $slug ] : '';
);
然后,您可以像這樣在php中使用它:
<form action="/go/<?= $item ?>/<?php echo $case1 ; ?>" method="POST" target="_blank">
<input name="a" type="hidden" value="<?php echo $a; ?>"/>
<input name="b" type="hidden" value="<?php echo $b; ?>"/>
<input name="c" type="hidden" value="<?php echo $c; ?>"/>
<input name="d" type="hidden" value="<?php echo $d; ?>"/>
<input type="submit" class="<?= get_class( $case1 ); ?> general-class" value="Click Me"></form>
雖然這似乎不是一個很大的價值,但是如果您開始將這些概念應用到代碼中,您將很快看到該價值開始累積。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.