我有一个表单,用户可以在我的页面上发布问题,我想限制表单描述部分的字符。如果我能找到一个单词计数器来工作,那也将是一个加号。
表格:
<p><label for="title">Titel</label><br />
<input type="text" id="title" value="" tabindex="1" size="20" name="title2" />
</p>
<p><?php wp_dropdown_categories( \'show_option_none=Kategorier&tab_index=4&taxonomy=register_vote_category&hide_empty=0\' ); ?></p>
<p><label for="description">Beskrivning</label><br />
<textarea id="description" tabindex="3" name="description2" cols="50" rows="6"></textarea>
</p>
<p><input type="submit" value="Skicka" tabindex="6" id="submit" name="submit2" /></p>
<input type="hidden" name="action" value="new_post" />
<?php wp_nonce_field( \'new-post\' ); ?>
</form>
php:
if(isset($_POST[\'submit2\'])) {
$title = $_POST[\'title2\'];
$description = $_POST[\'description2\'];
if(strlen($title) <= 0 || strlen($description) <= 0){
echo wpautop( \'Du måste skriva i både titel och beskrivning\' );
}
else if (strlen($title) >= 1 && strlen($description) >= 1) {
$new_post = array(
\'post_title\' => $title,
\'post_content\' => $description,
\'post_category\' => array($_POST[\'cat\']), // Usable for custom taxonomies too
\'post_status\' => \'publish\', // Choose: publish, preview, future, draft, etc.
\'post_type\' => \'category-vote\' //\'post\',page\' or use a custom post type if you want to
);
//save the new post
$pid = wp_insert_post($new_post);
//insert taxonomies
}
};
SO网友:APAD1
要限制字符数,请将其添加到文本区域:maxlength="200"
将“200”更改为您想要的字符限制。
<textarea id="description" maxlength="200" tabindex="3" name="description2" cols="50" rows="6"></textarea>
对于字符计数器,您需要一些基本Javascript,如下所示:
counter = function() {
var value = $(\'#description\').val();
if (value.length == 0) {
$(\'#totalChars\').html(0);
return;
}
var totalChars = value.length;
$(\'#totalChars\').html(totalChars); //change the selector to whatever the ID of your counter area is
};
$(document).ready(function() {
$(\'#description\').change(counter);
$(\'#description\').keydown(counter);
$(\'#description\').keypress(counter);
$(\'#description\').keyup(counter);
$(\'#description\').blur(counter);
$(\'#description\').focus(counter);
});