簡體   English   中英

在Javascript / Jquery中提取字符串部分

[英]Extract string part in Javascript / Jquery

我正在使用繭形寶石在工作時間內添加動態嵌套表格。

繭返回open_time和close_time輸入。

id = merchant_working_hours_attributes_ID_open_time
id = merchant_working_hours_attributes_ID_close_time

在CSS上,close_time輸入定義為display:none,而我的想法只是在open_time輸入標記為nil的情況下顯示。

但是為此,我需要從open_time輸入中提取ID並根據該ID將css樣式添加到close_time輸入中。

如何使用javascript / Jquery實現呢? 還有其他形式可以實現嗎?

您的問題是說您有一個字符串,並且想要提取各個元素的ID以執行樣式。

使用split()

var string = merchant_working_hours_attributes_ID_open_time;

var parts = string.split("_"); //You will have the ID at 4th parts[4]

根據提取的ID,可以使用show()hide()

if($('#opentimeID').val() != '')
{
  $('#opentimeID').show();
}

使用substring()

string.substring(start,end)

這應該相對簡單。 我的假設是,默認情況下,“關閉時間”字段未顯示。 但是,當您開始在“開放時間”字段中輸入值時,“關閉時間”字段就會顯示出來。

如果是這樣,這應該為您工作。

的JavaScript

const openTime = document.getElementById('merchant_working_hours_attributes_ID_open_time')
const closeTime = document.getElementById('merchant_working_hours_attributes_ID_close_time')
if (openTime.value.length > 0) {
  closeTime.style.display = 'block'
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM