简体   繁体   English

为了使我的character_count工作,我需要对HTML和JavaScript代码进行哪些更改?

[英]What changes to my HTML and JavaScript code do I need to make, in order for my character_count to work?

I am currently working on a WordPress website, with WooCommerce functionality. 我目前正在使用WooCommerce功能的WordPress网站上工作。

One of my tasks is to insert a Custom Text Field on the Product Page, where shoppers are able to assign a piece of text they would like to see on their Product. 我的任务之一是在产品页面上插入自定义文本字段,购物者可以在其中分配他们希望在产品上看到的一段文本。 To speed up the process, I have used a Plugin. 为了加快该过程,我使用了一个插件。 To generate this Custom Field, the Plugin Outputs the following code on the Product Page: 为了生成此自定义字段,插件在产品页面上输出以下代码:

HTML: HTML:

<div class="fields-group-1">    

    <table class="fields_table  product-custom-text-wrapper" cellspacing="0">
        <tbody>
            <tr>
                <td class="label"><label class=" product-custom-text-label" for="custom_text">Custom Text</label></td>
                <td class="value">
                    <input type="text" class="field product-custom-text" name="custom_text" value="" placeholder="Enter Custom Text ..." maxlength="16" type="text" pattern="mandatory" mandatory="no"  />
                    <span class="validation-message is-valid-1"></span>
                </td>
            </tr>
        </tbody>
    </table>

</div>

I would now like to Output the Character Count to the Product Page. 我现在想将字符计数输出到产品页面。 After browsing the net, I can see the following code would help with this: 浏览完网络后,我可以看到以下代码将对此有所帮助:

JavaScript 的JavaScript

$('textarea').keyup(updateCount);
$('textarea').keydown(updateCount);

function updateCount() {
    var cs = $(this).val().length;
    $('#character_count').text(cs);
}

At present, the code does not work. 目前,该代码不起作用。 Using the HTML above, is anyone aware what I would need to change the 'textarea' in the JavaScript code to, in order for this code to work? 使用上面的HTML,有人知道我需要将JavaScript代码中的“ textarea”更改为该代码才能正常工作吗?

Once I have got the code working, I can then simply enter <span id="character_count"></span> for the code to be complete. 代码正常工作后,我只需输入<span id="character_count"></span>即可完成代码。

Your jQuery selector $('textarea') is incorrect as there is no textarea in your HTML. 您的jQuery选择器$('textarea')不正确,因为您的HTML中没有textarea。 It looks like you want to use $('.product-custom-text') instead. 看来您想使用$('.product-custom-text') Of course it would be better to select it using an ID rather than a class by adding an ID to your input tag. 当然,最好是使用ID而不是通过在输入标签中添加ID来选择类。

The selectors for jQuery are the same for CSS. jQuery的选择器与CSS相同。 So if you want to exactly select an element, use it's ID. 因此,如果您想精确选择一个元素,请使用它的ID。

Your code becomes: 您的代码变为:

$('.product-custom-text').keyup(updateCount);
$('.product-custom-text').keydown(updateCount);

function updateCount() {
    var cs = $(this).val().length;
    $('#character_count').text(cs);
}

EDIT: 编辑:

To include a character count only in products which have the text box, I would include it in the same scope of the table and use CSS to position it correctly. 为了仅在具有文本框的产品中包含字符计数,我将其包含在表的同一范围内,并使用CSS正确放置它。

For example: 例如:

 $('.product-custom-text').keyup(updateCount); $('.product-custom-text').keydown(updateCount); function updateCount() { var cs = $(this).val().length; $(this).next().next().text(cs); } 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <div class="fields-group-1"> <table class="fields_table product-custom-text-wrapper" cellspacing="0"> <tbody> <tr> <td class="label"><label class=" product-custom-text-label" for="custom_text">Custom Text</label></td> <td class="value"> <input type="text" class="field product-custom-text" name="custom_text" value="" placeholder="Enter Custom Text ..." maxlength="16" type="text" pattern="mandatory" mandatory="no" /> <span class="validation-message is-valid-1"></span> <span id="character_count"></span> </td> </tr> <tr> <td class="label"><label class=" product-custom-text-label" for="custom_text">Custom Text</label></td> <td class="value"> <input type="text" class="field product-custom-text" name="custom_text" value="" placeholder="Enter Custom Text ..." maxlength="16" type="text" pattern="mandatory" mandatory="no" /> <span class="validation-message is-valid-1"></span> <span id="character_count"></span> </td> </tr> </tbody> </table> </div> 

This update contradicts my former part about wanting to select with an ID, in this case we ant to select by class as there are multiple products on the same page. 此更新与我之前希望使用ID选择的部分相矛盾,在这种情况下,由于同一页面上有多个产品,因此我们希望按类别选择。

Take note of the .next().next() portion of the code. 注意代码的.next().next()部分。 This skips the validation message span and grabs the span for the character count. 这将跳过验证消息的跨度,并获取字符计数的跨度。 This can be optimized but works for now. 可以对其进行优化,但现在可以使用。 You can try it live in the example window. 您可以在示例窗口中尝试使用它。

You do not have any <textarea> s on the page. 您的页面上没有任何<textarea> Add an id="whatever" attribute to the <input /> element, and change the $('textarea') selectors to $('#whatever') . <input />元素中添加一个id="whatever"属性,并将$('textarea')选择器更改$('#whatever')

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

相关问题 我在HTML中缺少什么才能使JavaScript工作? - What am I missing in my html to make the javascript work? 如何使网站上的Javascript代码与Firefox兼容 - What can I do to make the Javascript code on my site work with Firefox 我需要在我的代码中更改哪些模式才能在我的登录按钮上工作? - What do I need to change in my code for the modals to work on my log in button? 我在 redux 上写标签时犯了什么错误? 我需要在我的代码中更改什么? - What mistakes did I make in writing tabs on redux? What do I need to change in my code? 我需要添加什么,所以我的JavaScript会像我想要的那样工作? - What do i need to add, so my JavaScript would work as i want it to? 我无法让我的 JavaScript 代码在我的 HTML 文件中工作 - I can not get my JavaScript code to work in my HTML file 我需要使用HTML / JavaScript代码进行一些故障排除 - I need some troubleshooting with my HTML/JavaScript code 为什么我的下一个和上一个控件在分页时无法采用正确的索引? 我需要做哪些改变? - Why my next and previous control not able to take the correct index when paginating? What changes do I need to make? 我是否需要命名我的JavaScript代码,如果它在一个 <iframe> ? - Do I need to namespace my JavaScript code if it's in an <iframe>? 我需要在HTML(JSON响应)中转义什么 - What do I need to escape in my HTML (JSON response)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM