简体   繁体   English

Jquery $(this).val()不起作用

[英]Jquery $(this).val() not working

I am having issues using jquery to set the value a element which calls a function. 我在使用jquery将值设置为调用函数的元素时遇到问题。 Here is the code 这是代码

Element: 元件:

 each outlet in FBoutlets  
        br
        label(for='FBoutlet') Facebook: #{outlet.FBoutlets.name}  
        input(type='checkbox', value='', onchange="streamFB(" + outlet.FBoutlets.FBpageId + "," + JSON.stringify(outlet.FBoutlets.FBaccessToken) + ")", name='FBoutletCredentials', id='FBoutlet')

Function: 功能:

  script.
    window.fbAsyncInit  = function()  {
    FB.init({
      appId             : '20212222222',
      autoLogAppEvents  : true,
      xfbml             : true,
      version           : 'v2.12'
      });
      };

    function streamFB(pageId, accessToken){
      FB.api(
        '/' + pageId + '/live_videos',
        'POST',
        {access_token: accessToken},
        function(response) {
            let rtmp = response.stream_url
            console.log(rtmp)
            console.log(response)
            $(this).val() = rtmp;
        }
      );
    }

$(this).val() is returning undefined, I need to use this because the element id dynamically generated $(this).val()返回undefined,我需要使用它,因为元素id是动态生成的

You should change it 你应该改变它

$(this).val() = rtmp;

to

$(this).val(rtmp);

jquery val function takes argument value as assigning value jquery val函数将参数值作为赋值

Definition and Usage 定义和用法

The val() method returns or sets the value attribute of the selected elements. val()方法返回或设置所选元素的value属性。

When used to return value: 用于返回值时:

This method returns the value of the value attribute of the FIRST matched element. 此方法返回FIRST匹配元素的value属性的值。

When used to set value: 用于设置值时:

This method sets the value of the value attribute for ALL matched elements. 此方法为所有匹配的元素设置value属性的值。

Note: The val() method is mostly used with HTML form elements. 注意:val()方法主要用于HTML表单元素。

Syntax 句法

Return the value attribute: 返回value属性:

$(selector).val()

Set the value attribute: 设置值属性:

$(selector).val(value)

Set the value attribute using a function: 使用函数设置value属性:

$(selector).val(function(index,currentvalue));

Parameter Description 参数说明

value Required. 值必需。 Specifies the value of the value attribute function(index,currentvalue) Optional. 指定value属性函数的值(index,currentvalue)可选。 Specifies a function that returns the value to set. 指定返回要设置的值的函数。 index - Returns the index position of the element in the set currentvalue - Returns the current value attribute of selected elements index - 返回set currentvalue中元素的索引位置 - 返回所选元素的当前值属性

source: https://www.w3schools.com/jquery/html_val.asphttps://www.w3schools.com/jquery/html_val.asp 来源: https//www.w3schools.com/jquery/html_val.asphttps//www.w3schools.com/jquery/html_val.asp

Alternate solutions, 替代解决方案,

$(this)[0].value = rtmp;

or 要么

this.value = rtmp;

I think you have to update $(this).val() = rtmp; 我想你必须更新$(this).val()= rtmp; to - 至 -

$(this).val(rtmp);

Or, 要么,

this.value = rtmp;

Change $(this).val() = rtmp; 改变$(this).val() = rtmp; to $(this).val(rtmp); to $(this).val(rtmp);

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

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